package cn.xuan.scan.hello.hcontroller;

import java.util.HashMap;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.DefaultPasswordService;
import org.apache.shiro.authc.credential.PasswordService;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController {
	private static final Logger logger = LoggerFactory.getLogger(UserController.class);

	@ResponseBody
	@RequestMapping("register")
	public Map<String, Object> register() {
		// 
		PasswordService passwordService = new DefaultPasswordService();

		String pwd = passwordService.encryptPassword("123456");

		UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123");
		token.setRememberMe(true);
		String rand = (String) SecurityUtils.getSubject().getSession().getAttribute("rand");
		
		

		logger.info("pwd11111111===>" + pwd);
		// 生成盐（部分，需要存入数据库中）
		String random = new SecureRandomNumberGenerator().nextBytes().toHex();

		logger.info("random===>" + random);
		// 将原始密码加盐（上面生成的盐），并且用md5算法加密三次，将最后结果存入数据库中
		String result = new Md5Hash("123", random, 3).toString();
		logger.info("pwd===>" + result);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pages", "register");
		logger.info(com.alibaba.druid.support.json.JSONUtils.toJSONString(map));
		return map;
	}

	@ResponseBody
	@RequestMapping("users")
	public Map<String, Object> users() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pages", "users");
		logger.info(com.alibaba.druid.support.json.JSONUtils.toJSONString(map));
		return map;
	}

	@ResponseBody
	@RequestMapping("logout")
	public Map<String, Object> logout() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pages", "logout");
		logger.info(com.alibaba.druid.support.json.JSONUtils.toJSONString(map));
		return map;
	}
}
